﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HFController;

namespace HFWebManage.Instruments
{
    public partial class Instruments : HFController.BagePages
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //判断管理员有没有登录，如果没登录，踢出
            this.checkAdminIsLogin();

            //是否首次加载
            if (!Page.IsPostBack)
            {
                this.bindInstrumentsBigClass();
                DropSmallClassID.Items.Insert(0,new ListItem("--请选择--","0"));
                this.bindInstrumentsFileType();

                //绑定省份城市
                DropCityID.Items.Insert(0, new ListItem("--请选择--", "0"));
                this.bindProvinceID();

                //获取Action参数，判断是修改还是删除
                string Action = this.GetQuery("action");

                switch (Action)
                {
                    case "modify":
                        //表示修改
                        this.bindmodel();
                        break;
                    case "delete":
                        //表示删除
                        int ID = int.Parse(this.GetQuery("ID"));
                        InstrumentsController.getController().doDelegeInstruments(ID);
                        break;
                }
            }
        }

        public void bindProvinceID()
        {
            DropProvinceID.DataSource = CityController.getController().getProvinceList(100);
            DropProvinceID.DataValueField = "ID";
            DropProvinceID.DataTextField = "ProvinceName";
            DropProvinceID.DataBind();
            DropProvinceID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        public void bindCityID()
        {
            DropCityID.DataSource = CityController.getController().getCityList(100, DropProvinceID.SelectedValue);
            DropCityID.DataValueField = "ID";
            DropCityID.DataTextField = "CityName";
            DropCityID.DataBind();
            DropCityID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        /// <summary>
        /// 定义一个方法，绑定文书大分类
        /// </summary>
        private void bindInstrumentsBigClass()
        {
            dropClassID.DataSource = InstrumentsController.getController().getInstrumentsBigClassList(100);
            dropClassID.DataValueField = "ID"; 
            dropClassID.DataTextField = "InstrumentsName"; 
            dropClassID.DataBind(); 
            dropClassID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        /// <summary>
        /// 文书大类选择框发生改变的时候触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void dropClassID_SelectedIndexChanged1(object sender, EventArgs e)
        {
            this.bindInstrumentsSmallClass(int.Parse(this.dropClassID.SelectedValue));
        }

        /// <summary>
        /// 定义一个方法，绑定文书小分类
        /// </summary>
        private void bindInstrumentsSmallClass(int ID)
        {
            DropSmallClassID.DataSource = InstrumentsController.getController().getInstrumentsSmallClassList(ID ,1000);
            DropSmallClassID.DataValueField = "ID"; 
            DropSmallClassID.DataTextField = "InstrumentsName"; 
            DropSmallClassID.DataBind(); 
            DropSmallClassID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        /// <summary>
        /// 定义一个方法，绑定文分类类型
        /// </summary>
        private void bindInstrumentsFileType()
        {
            DropTypeID.DataSource = InstrumentsController.getController().getInstrumentsFileTypeList(100);
            DropTypeID.DataValueField = "ID"; 
            DropTypeID.DataTextField = "TypeName"; 
            DropTypeID.DataBind(); 
            DropTypeID.Items.Insert(0, new ListItem("--请选择--", "0"));
        }

        /// <summary>
        /// 绑定修改
        /// </summary>
        private void bindmodel()
        {
            int ID = int.Parse(this.GetQuery("ID"));
            //得到这条记录
            hf.Model.Instruments model = InstrumentsController.getController().getInstrumentsModel(ID);
            dropClassID.SelectedValue = model.ClassID.ToString();
            DropSmallClassID.SelectedValue = model.SmallClassID.ToString();
            DropTypeID.SelectedValue = model.TypeID.ToString();
            tbTitle.Text = model.Title;
            tbAddtime.Text = model.Addtime.ToString();
            tbContent.Text = model.Content;

            hf.Model.City city = CityController.getController().getCityList().Find(i => i.ID == model.CityID);

            DropProvinceID.SelectedValue = CityController.getController().getProvinceList().Find(i => i.ID == city.ProvinceID).ID.ToString();

            DropCityID.DataSource = CityController.getController().getCityList(100, DropProvinceID.SelectedValue);
            DropCityID.DataValueField = "ID";
            DropCityID.DataTextField = "CityName";
            DropCityID.DataBind();
            DropCityID.Items.Insert(0, new ListItem("--请选择--", "0"));

            DropCityID.SelectedValue = model.CityID.ToString();

            //隐藏域ID赋值
            IDS.Value = model.ID.ToString();
        }
            

        /// <summary>
        /// 保存文书
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            string Action = this.GetQuery("action");
            if (Action == "modify")
            {
                InstrumentsController.getController().doModifyInstruments(int.Parse(IDS.Value),
                    dropClassID.SelectedValue,
                    DropSmallClassID.SelectedValue,
                    DropTypeID.SelectedValue,
                    tbTitle.Text,
                    tbContent.Text,
                    DateTime.Parse(tbAddtime.Text),
                    int.Parse(DropCityID.SelectedValue)
              );
            }
            else
            {
                InstrumentsController.getController().doAddInstruments(dropClassID.SelectedValue,
                     DropSmallClassID.SelectedValue,
                     DropTypeID.SelectedValue,
                     tbTitle.Text,
                     tbContent.Text,
                     DateTime.Parse(tbAddtime.Text),
                    int.Parse(DropCityID.SelectedValue)
                     );
            }
        }

        protected void DropProvinceID_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.bindCityID();
        }
        
    }
}